#include "game_client.h"

#include <map>

LPDIRECT3D9 g_d3d = NULL;
HWND hWnd = NULL;
//extern HINSTANCE hinstance;
extern void Setup_input();
extern IDirect3DDevice9 * cur_device;

map<unsigned int, int> formatCheck;
/// check device format locally
void checkAllDeviceFormat(){
	unsigned int format[]={
		111, 28, 26, 22, 21, 23, 81, 827611204, 861165636, 894720068,
		50, 51, 114, 113, 25,35,36, 116, 115, 112, -1, 909198916, 
		875710020, 1515474505, 1515667794,
		-65536

	};
	unsigned int usage[]={
		1048576, 0, 65536, 262144, 1, 2,
		-65536
	};
	unsigned int surfaceFormat[] =  {1280070990, 1515406674}; // resource type is 1, the others are 3
}



void DestroyD3DWindow() {
	printf("Destroying D3D window...");

	if(g_d3d != NULL) g_d3d->Release();
	g_d3d = NULL;
}

LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
	switch(msg) {
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
		break;

	case WM_KEYUP:
		if(wp == VK_ESCAPE) PostQuitMessage(0);
		break;
	}

	return DefWindowProc(hWnd, msg, wp, lp);
}
extern HWND CreateWindowWithSDL(int w, int h, int x,int y);
bool init_window(int width, int height) {
	printf("Server init window...\n");
	Log::log("client_init::init_window(), width=%d, height=%d\n", width, height);
	WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0, 0, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, WINDOW_CLASS, NULL };
	RegisterClassEx(&wc);

	int screenWidth = GetSystemMetrics(SM_CXSCREEN);
	int screenHeight = GetSystemMetrics(SM_CYSCREEN);

	// Create the application's window
	/*hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW, 0, 0, width, height, GetDesktopWindow(), NULL, wc.hInstance, NULL);*/
	if( width < 800)
		width = 800;
	if(height < 600)
		height = 600;
	hWnd = CreateWindowWithSDL(width, height, 0, 0);
	//hinstance = wc.hInstance;
	//Setup_input();
	return true;
}
D3DDISPLAYMODE displayMode;
D3DPRESENT_PARAMETERS d3dpp;
HRESULT server_init() {
	printf("Init Direct3D in server...\n");

	if(g_d3d == NULL) {
		g_d3d = Direct3DCreate9(D3D_SDK_VERSION);
	}

	
	if(FAILED(g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode))) return false;

	/////////////////////////////////////////////
	//read data
	int id = cc.read_int();
	UINT Adapter = cc.read_uint();
	D3DDEVTYPE DeviceType = (D3DDEVTYPE)(cc.read_uint());
	DWORD BehaviorFlags = cc.read_uint();
	
	cc.read_byte_arr((char*)(&d3dpp), sizeof(d3dpp));
	d3dpp.BackBufferFormat = displayMode.Format;
	/////////////////////////////////////////////

	Log::log("server_init(), init_window start, Presentation Parameter back buffer witth:%d, back buffer height:%d\n", d3dpp.BackBufferWidth, d3dpp.BackBufferHeight);
	d3dpp.BackBufferHeight = 600;
	d3dpp.BackBufferWidth = 800;

	//init_window(d3dpp.BackBufferWidth, d3dpp.BackBufferHeight);
	HWND hh = FindWindow(NULL,"game-client");
	if(hh == NULL){
		printf("error to find window\n");
	}
	d3dpp.hDeviceWindow = hh;
	hWnd = hh;

	LPDIRECT3DDEVICE9 base_device = NULL;
	HRESULT hr = g_d3d->CreateDevice(Adapter, DeviceType, hWnd, BehaviorFlags, &d3dpp, &base_device);
	device_list[id] = base_device;
	cur_device= base_device;

	switch(hr){
	case D3D_OK:
		Log::log("client_init(), create devie return D3D_OK\n");
		break;
	case D3DERR_DEVICELOST:
		Log::log("client_init(), create device return D3DERR_DEVICELOST.\n");
		break;
	case D3DERR_INVALIDCALL:
		Log::log("client_init(), create devie return D3DERR_INVALIDCALL\n");
		break;
	case D3DERR_NOTAVAILABLE:
		Log::log("client_init(), create device return D3DERR_NOTAVAILABLE.\n");
		break;
	case D3DERR_OUTOFVIDEOMEMORY:
		Log::log("client_init(), create device return D3DERR_OUTOFVIDEOMEMORY.\n");
		break;
	default:
		break;
			
	}
	if(base_device == NULL) {
		Log::log("client_init(), device is NULL, id=%d\n", id);
	}
	else {
		Log::log("client_init(), device is good, id=%d\n", id);
	}

	Log::log("server_init(), CreateDevice end\n");

	ShowWindow(hWnd, SW_SHOWNORMAL);
	UpdateWindow(hWnd);
	atexit(DestroyD3DWindow);

	return hr;
}


